<?php if (!defined('insite')) die("no access"); 
if (defined('insite'))
{
  if (adm_check($_SESSION["user"])==1)
  {
	$button = "name='addlink' value=".dl_manager_bt1."";
	$titleZ="";
	$ggiz="";
	$linki="";
	$glin="";
	$filedl = @file("_dat/dl.dat");
	$linkZ = "_dat/dl.dat";?>
	<table width='90%'  class='tableMain' align='center' vAlign='center'>
	<tr class='trstyle'>
		<td class='zagstyle'><?=dl_manager_title?></td>
	</tr>
	<tr>
		<td align='center'><?=dl_manager_already?></td>
	</tr>
  
  <?
	if ($_REQUEST["addlink"])
		{
			$titleZ = htmlspecialchars($_POST["title"]);
			$glin = htmlspecialchars($_POST["nlink"]);
			$ggiz = htmlspecialchars($_POST["opis"]);
			$linki = $_POST["links"];
			if(strlen($titleZ)>1 && strlen($glin)>1 && strlen($ggiz)>1 && strlen($linki)>1)
			{
				$button = "name='addlink' value=".dl_manager_bt1."";		
				$dhandler = fopen($linkZ,"a+");
				if (filesize($linkZ) > 0 ){fwrite($dhandler,"\r\n".$titleZ."||".$ggiz."||".$linki."||".$glin."||0||0");}
				else{fwrite($dhandler,$titleZ."||".$ggiz."||".$linki."||".$glin."||0||0");}
				fclose($dhandler);
			}
			header("location:".$config["siteaddress"]."/index.php?p=usercp&ss=down");		
		}
	if($_GET["edit"]==1 && isset($_GET["pos"]) && !$_REQUEST["editlink"])
	{	
		$position = checknum(substr($_GET["pos"],0,3));
		$temp = explode("||",$filedl[$position]);
		$titleZ=$temp[0];$ggiz=$temp[1];$linki=$temp[2];$glin=$temp[3];
		$button = "name='editlink' value=".dl_manager_bt2."";
	}
	elseif($_GET["edit"]==1 && $_REQUEST["editlink"])
	{
		$button = "name='addlink' value=".dl_manager_bt1."";
		$position = checknum(substr($_GET["pos"],0,3));
		$titleZ = htmlspecialchars($_POST["title"]);
		$glin = htmlspecialchars($_POST["nlink"]);
		$ggiz = htmlspecialchars($_POST["opis"]);
		$linki = $_POST["links"];
		if(strlen($titleZ)>1 && strlen($glin)>1 && strlen($ggiz)>1 && strlen($linki)>1)
			{
				if($position ==(count($filedl)-1) ){$filedl[$position]= $titleZ."||".$ggiz."||".$linki."||".$glin."||0||0";}
				else {$filedl[$position]= $titleZ."||".$ggiz."||".$linki."||".$glin."||0||0".chr(13).chr(10);}
				$dhandler = fopen($linkZ,"w");
				for ($i=0; $i<count($filedl);$i++){fwrite($dhandler,$filedl[$i]);}fclose($dhandler);
			}
		header("location:".$config["siteaddress"]."/index.php?p=usercp&ss=down");
	}
	if($_GET["edit"]==0 && isset($_GET["pos"]))
	{
		$position = checknum(substr($_GET["pos"],0,3));
		unset($filedl[$position]);
		$dhandler = fopen($linkZ,"w");
		for ($i=0; $i<count($filedl);$i++)
		{
			if (!$filedl[$i]){ $i++; fwrite($dhandler,$filedl[$i]);}
			else {fwrite($dhandler,$filedl[$i]);}
		}
		fclose($dhandler);
		$button = "name='addlink' value=".dl_manager_bt1."";$titleZ="";$ggiz="";$linki="";$glin="";
		header("location:".$config["siteaddress"]."/index.php?p=usercp&ss=down");
	}
	if(count($filedl)> 0 or $filedl)
	{
		$cs=0;
		if(!$filedl or empty($filedl))
		{?><div align='center' class='warnms'><?=donwn_empty?></div><?}
		else
		{
			foreach ($filedl as $f=>$n)
			{
				$tempa = explode("||",$n);
				if ($tempa[4]==1){$tempa[4]=dl_manager_msg;}else{$tempa[4]="";}
				if (substr(trim($tempa[3]),0,4)!="http") $tempa[3] = "http://".$tempa[3];
				?>
				<tr>
					<td><hr>
						<table width="100%" align="center" border="0">
						<tr>
							<td class="zagstyle"><?=$tempa[0]?></td><td class="obs"><?=$tempa[1]?></td><td><a href="<?=$tempa[3]?>" target='_blank'><?=$tempa[2]?></a> <span class="warnms"><?=$tempa[4]?></span></td>
						</tr>
						<tr>
							<td colspan="3"> <a href="<?=$config["siteaddress"]?>/index.php?p=usercp&ss=down&edit=1&pos=<?=$cs?>"><img src='imgs/edit.png' title='edit' border='0'></a><a href="<?=$config["siteaddress"]?>/index.php?p=usercp&ss=down&edit=0&pos=<?=$cs?>"><img src="imgs/delete.png" title="delete" border="0"></a></td>
						</tr>
						</table>
					</td>
				</tr>
				<? $cs++;
			}
		}
	}
	else{?> <tr><td align='center'>Empty</td></tr><?}
	?><tr>
			<td>
			<form action="" method="post">
			<fieldset><legend class="warnms"><?=dl_manager_optinos?></legend>
			<table width="100%" align="center" border="0">
				<tr><td><?=dl_manager_etitle?></td><td><input class='combx1' type='text' name='title' value='<?=$titleZ?>'></td></tr>
				<tr><td><?=dl_manager_emain?></td><td><input class='combx1' type='text' name='opis' value='<?=$ggiz?>'></td></tr>
				<tr><td><?=dl_manager_elinkZ?></td><td><input class='combx1' type='text' name='nlink' value='<?=$glin?>'></td></tr>
				<tr><td><?=dl_manager_nik?></td><td><input class='combx1' type='text' name='links' value='<?=$linki?>'></td></tr>
				<tr><td colspan="2" align="center"><input type="submit" class='t-button' <?=$button?>></td></tr>
			</table></fieldset>
			</form>
			</td>
		</tr>
		</table><?
  }else {die();} 
}
else die();?> 